const repl = require('repl');
const mqtt = require('mqtt');

class Test {
  constructor() {
    this.mqtt = mqtt.connect('mqtt://localhost:1883');
    this.repl = repl.start('');
    this.init();
  }
  init() {
    this.pubMsg('bookstart')
    this.repl.on('exit', () => {
      console.log('Exit!');
      process.exit(0)
    });
    this.repl.defineCommand('pageflip', {
      help: 'pageflip',
      action: () => {
        this.pubMsg('pageflip')
      }
    });
    this.repl.defineCommand('start', {
      help: 'bookstart',
      action: () => {
        this.pubMsg('bookstart')
      }
    });
    this.repl.defineCommand('stop', {
      help: 'bookstop',
      action: () => {
        this.pubMsg('bookstop')
      }
    });
    this.repl.defineCommand('long', {
      help: 'longpress',
      action: () => {
        this.pubMsg('longpress')
      }
    });
    this.repl.defineCommand('short', {
      help: 'shortpress',
      action: () => {
        this.pubMsg('shortpress')
      }
    });
  }
  pubMsg(msg) {
    this.mqtt.publish('ra/command', JSON.stringify({
      'command': msg
    }))
  }
}

new Test();